<?php
/**
 * A set of utility functions to manipulate strings
 * 
 * @package be.appcraft.util
 * @version $Revision: 1.4 $
 * 
 * @author Stijn Van Bael
 * @since 1.0
 */
class Pageley_Util_String {
	private function __construct() { }
	
	/**
	 * Tests whether a string starts with a certain part or not.
	 *
	 * @param string $string: the string to contain the part
	 * @param string $start: the part to find in the string
	 * @return true when the string starts with the part, false otherwise
	 */
	public static function startsWith($string, $start) {
		$pos = strpos($string, $start);
		return $pos === 0 && $pos !== false;
	}
	
	/**
	 * Tests whether a string ends with a certain part or not.
	 *
	 * @param string $string: the string to contain the part
	 * @param string $end: the part to find in the string
	 * @return true when the string ends with the part, false otherwise
	 */
	public static function endsWith($string, $end) {
		$pos = strrpos($string, $end);
		return $pos !== false && $pos === strlen($string) - strlen($end);
	}
	
	/**
	 * Returns a part of a string
	 *
	 * @param string $string: the string to extract the part from
	 * @param int $start: the zero based start index of the substring
	 * @param int $end: the zero based end index of the substring, or null
	 * to return the remainder of the string
	 * @return the substring of the specified string, starting from the start
	 * index and ending at the end index
	 */
	public static function substring($string, $start, $end = -1) {
		if($end === -1) {
			$end = strlen($string) - $start;
		}
		return substr($string, $start, $end);
	}
	
    public static function substringBefore($string, $fragment) {
        $pos = self::indexOf($string, $fragment);
        if($pos === false) {
            throw new Pageley_Util_IllegalArgumentException("'" . $fragment . "' does not occur within '" . $string . "'.");
        }
        return self::substring($string, 0, $pos);	
    }

    public static function substringAfter($string, $fragment) {
        $pos = self::indexOf($string, $fragment);
        if($pos === false) {
            throw new Pageley_Util_IllegalArgumentException("'" . $fragment . "' does not occur within '" . $string . "'.");
        }
        return self::substring($string, $pos + self::length($fragment));
    }
    
	/**
	 * Returns the number of characters in a string
	 *
	 * @param string $string: the string
	 * @return the number of characters in the string
	 */
	public static function length($string) {
		return strlen($string);
	}
	
	/**
	 * Replaces all occurences of a substring within a string by another
	 * string.
	 *
	 * @param string $string the string to replace withing
	 * @param string $toReplace the substring to replace
	 * @param string $replacement the replacement
	 * @return the new string
	 */
	public static function replace($string, $toReplace, $replacement) {
		return str_replace($toReplace, $replacement, $string);
	}
	
	public static function toUpperCase($string) {
		return strtoupper($string);		
	}
	
	public static function toLowerCase($string) {
		return strtolower($string);		
	}
	
	public static function capitalize($string) {
		return self::toUpperCase(self::substring($string, 0, 1)) . self::substring($string, 1);
	}
	
	public static function uncapitalize($string) {
		return self::toLowerCase(self::substring($string, 0, 1)) . self::substring($string, 1);
	}
	
	public static function indexOf($string, $substring, $position = 0) {
		return strpos($string, $substring, $position);
	}
	
	public static function lastIndexOf($string, $substring) {
	    $lastPos = false;
	    while(($pos = self::indexOf($string, $substring, $lastPos + 1)) != false) {
	    	$lastPos = $pos;
	    }
	    return $lastPos;
	}
	
	public static function reverse($string) {
		return strrev($string);
	}
	
	public static function contains($string, $substring) {
		return !(self::indexOf($string, $substring) === false);
	}
    
    public static function split($string, $pattern) {
        return new Pageley_Util_ArrayMap(split($pattern, $string));
    }
	
	public static function addUnderscores($camelCaseText) {
		$underscoreText = "";
		$first = true;
		for($i = 0; $i < self::length($camelCaseText); $i++) {
			$char = self::substring($camelCaseText, $i, 1);
			if(ord($char) > 64 && ord($char) < 91) {
				if(!$first) {
					$underscoreText .= "_";
				}
				$underscoreText .= self::toLowerCase($char);
			} else {
				$underscoreText .= $char;
			}
			$first = false;
		}
		return $underscoreText;
	}
}